package classes;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import ontology.UMCourtLOntology;
import ontology.actions.general.AddNewResult;
import ontology.actions.general.AddNewTime;
import ontology.concepts.general.Result;

import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.content.onto.basic.Action;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;

public class UMCourtAgent extends Agent {

	private Codec codec = new SLCodec();
	private Ontology ontology = UMCourtLOntology.getInstance();
	protected MessageTemplates mt = new MessageTemplates();
	protected Messenger msn = new Messenger();
	protected AIDs aids = new AIDs();
	private static Connection C;
	
	public void println(String s){System.out.println(s);}
	public void myprintln(String s){System.out.println("*****"+this.getLocalName()+": "+s);}
	
	public void sendNewResultMessage(Result r)
	{
		AddNewResult nr = new AddNewResult();
		nr.setNewResult(r);
		
		ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
		msg.setLanguage(codec.getName());
		msg.setOntology(ontology.getName());
		
		try 
		{
			getContentManager().fillContent(msg, new Action(aids.EVALUATOR, nr));
			msg.addReceiver(aids.EVALUATOR);
			send(msg);
		}
		catch (Exception ex) { ex.printStackTrace(); }
	}
	

	protected void setup()
	{
		mt = new MessageTemplates();
		msn = new Messenger();
		aids = new AIDs();
	}
	
	protected void registerAgent(String serviceName)
	{
		try 
		{
		    DFAgentDescription dfd = new DFAgentDescription();
		    dfd.setName(getAID());
		    ServiceDescription sd  = new ServiceDescription();
	        sd.setType(serviceName);
		    DFService.register(this, dfd);
		    System.out.println(getLocalName()+" registered with the DF");
		} 
		catch (FIPAException e) 
		{
		    e.printStackTrace();
		}
	}
	
	protected String getDate()
	{
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		java.util.Date date = new java.util.Date();
		return(dateFormat.format(date));
	}
	
	protected void takeDown()
	{
		System.out.println(getDate()+": "+this.getLocalName() +" is de-registering...");
		try { DFService.deregister(this); }
        catch (Exception e) {}
	}
	
	public void sendNewTime(String id, long sTime)
	{
		AddNewTime ant = new AddNewTime();
		ant.setId(id);
		ant.setTime((int) (System.currentTimeMillis()-sTime));
		
		ACLMessage m = new ACLMessage(ACLMessage.REQUEST);
		m.setLanguage(codec.getName());
		m.setOntology(ontology.getName());
		m.setConversationId(""+System.currentTimeMillis());
		
		try 
		{
			getContentManager().fillContent(m, new Action(aids.EVALUATOR, ant));
			m.addReceiver(aids.EVALUATOR);
			send(m);
		}
		catch(Exception e){e.printStackTrace();}
	}
	
	protected Connection connectDB()
	{
		//Connect to Database
		try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
	    catch (Exception E) 
	    {
	    	System.err.println("Unable to load driver");
	        E.printStackTrace();
	    }

	    try 
	    {
	    	C = DriverManager.getConnection("jdbc:mysql://tiaracserver.di.uminho.pt:3306","dave","d3LLdav#");
	    }
	    catch (SQLException E) 
	    {
	        System.out.println("SQLException: " + E.getMessage());
	        System.out.println("SQLState:     " + E.getSQLState());
	        System.out.println("VendorError:  " + E.getErrorCode());
	    }
	    
	    return C;
	}
}
